class MyArrayList {

    public int[] elem;
    public int usedSize;
    private static final int DEFAULT_SIZE = 10; // 默认容量

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
    private boolean isCross(int idx){
        return idx >= this.usedSize || idx < 0;
    }

    public boolean isFull(){
        return this.usedSize == this.elem.length;
    }

    public boolean isEmpty(){
        return this.usedSize == 0;
    }

    public void add(int data) {
        if (isFull()){
            this.elem = Arrays.copyOf(this.elem,this.usedSize*2);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }
    public void add(int pos, int data) {
        if (pos < 0 || pos > this.usedSize){
            System.out.println("pos下标不合法");
            return;
        }
        if (isFull()){
            this.elem = Arrays.copyOf(this.elem,this.usedSize*2);
        }

        for (int i = this.usedSize; i > pos; i--){
            elem[i] = elem[i-1];
        }
        elem[pos] = data;

        this.usedSize++;
    }
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    public int get(int pos) {
        if (isCross(pos)){
            System.out.println("垓下标不可用");
            return 0;
        }
        if (isEmpty()){
            System.out.println("数组元素为空");
            return 0;
        }
        return elem[pos];
    }
    public void set(int pos, int value) {
        if (pos > this.usedSize || pos < 0){
            System.out.println("垓下标不可用");
            return;
        }
        if (isFull()){
            this.elem = Arrays.copyOf(this.elem,this.usedSize*2);
        }
        elem[pos] = value;
    }
    public void remove(int toRemove) {
        int idx = -1;
        for (int i = 0; i < this.usedSize; i++){
            if (elem[i] == toRemove){
                idx = i;
                break;
            }
        }
        if (idx == -1){
            return;
        }
        for (int i = idx; i < this.usedSize - 1; i++){
            elem[i] = elem[i+1];
        }
        this.usedSize--;
    }
    public int size() {
        return this.usedSize;
    }
    public void clear() {
        for (int i = 0; i < this.usedSize; i++){
            elem[i] = 0;
        }
        this.usedSize = 0;
    }

}